/*
 * Main.java
 *
 * Created on __DATE__, __TIME__
 */

package tw.com.jinwei.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.Set;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.apache.log4j.Logger;

import tw.com.jinwei.db.Function;
import tw.com.jinwei.service.Login;
import tw.com.jinwei.utils.Constants;
import tw.com.jinwei.utils.ContextUtils;
import tw.com.jinwei.utils.LogHelper;

/**
 * 
 * @author __USER__
 */
public class MainFrame extends javax.swing.JFrame implements ActionListener {

	/** Creates new form Main */
	public MainFrame() {
		initComponents();
		this.setContentPane(desktop);
	}

	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		desktop = new JDesktopPane();
		desktop.setOpaque(true);
		add(desktop, BorderLayout.CENTER);

		menuBar = new javax.swing.JMenuBar();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setJMenuBar(menuBar);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 298,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>

	// GEN-END:initComponents

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JMenuBar menuBar;
	// End of variables declaration//GEN-END:variables

	private JLayeredPane desktop;

	private Logger log = LogHelper.getLog(getClass());
	private Login login;

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}

	public void initMenuBar() {
		if (log.isDebugEnabled()) {
			log.debug(new StringBuilder("initMenuBar"));
		}

		Set<Function> functions = login.getUser().getFunctions();

		if (log.isDebugEnabled()) {
			log.debug(new StringBuilder("function size == ").append(functions
					.size()));
		}

		for (Function f : functions) {
			if (f.getFunction() == null) {
				JMenu menu = new JMenu();
				menu.setText(f.getName());
				menu.setMnemonic(Integer.parseInt(f.getHotkey()));
				Set<Function> subFunctions = f.getFunctions();

				if (log.isDebugEnabled()) {
					log.debug(new StringBuilder("function[")
							.append(f.getName()).append(
									"] sub function size == ").append(
									subFunctions.size()));
				}

				for (Function sub : subFunctions) {
					if (sub.getType().equals("m") && functions.contains(sub)) {
						JMenuItem item;
						if (sub.getHotkey() != null) {
							item = new JMenuItem(sub.getName(), Integer
									.parseInt(sub.getHotkey()));
						} else {
							item = new JMenuItem(sub.getName());
						}
						item.addActionListener(this);
						menu.add(item);
					}
				}
				menuBar.add(menu);
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (log.isDebugEnabled()) {
			log.debug(new StringBuilder("acton command == ").append(e
					.getActionCommand()));
		}

		JInternalFrame frame = null;

		if (e.getActionCommand().equals(Constants.MENU_SYSTEM_USER_ADD)) {
			frame = (JInternalFrame) ContextUtils.getContext().getBean(
					"addUserFrame");
		}

		if (e.getActionCommand().equals(Constants.MENU_SYSTEM_USER_QUERY)) {
			frame = (JInternalFrame) ContextUtils.getContext().getBean(
					"queryUserFrame");
		}

		if (e.getActionCommand().equals(Constants.MENU_PRODUCT_CATEGORY_ADD)) {
			frame = (JInternalFrame) ContextUtils.getContext().getBean(
					"addProductCategoryFrame");
		}

		if (e.getActionCommand().equals(Constants.MENU_PRODUCT_CATEGORY_QUERY)) {
			frame = (JInternalFrame) ContextUtils.getContext().getBean(
					"queryProductCategoryFrame");
		}

		frame.setVisible(true);
		this.getContentPane().add(frame, 0);
		try {
			frame.setSelected(true);
			frame.setMaximum(true);
		} catch (PropertyVetoException e1) {
			log.error(new StringBuilder("set frame to selected error"), e1);
		}

	}
}